/****************************************************
 * 													*
 * 					BanzaiP2P						*
 * 													*
 * Versione: 0.1.4									*
 * Autore: D'Annessa Marco							*
 * File: banzai.h									*
 ****************************************************/
		
#include "client.h"
#include "console.h"


void client_start(){

	shared_files= create_list(conf->sharefolder);
	
	boot_peer = (struct peer*)malloc(sizeof(struct peer)* conf->bootpeernumber);
	struct http_rqst_msg msg;
	struct http_rspn_msg rspn;

	
	//ciclo console
	for(;;){
		
		menu_console();
		//choise=0;
		printf("scelta menù: ");
		scanf("%d",&choise);
		
		
		
		switch(choise){
			case 1:{//connect to BanzaiNet
				//prova bootstrap
				numconn=try_connection(conf->bootfile,conf->bootpeernumber, boot_peer);
				printf("Connected: %d",numconn);
			break;
			
			}
			case 2:{//show filelist
				file_list_console(shared_files);

				
			break;			
			}
			
			case 3:{//prova query
				
				http_rqst_msg_init(&msg,"0","7");
				http_rspn_msg_init(&rspn,"0","7");
				printf("E %s\n",msg.entity);
				printf("\n1Q: %s\n",query);
				printf("INSERISCI QUERY: ");
				
				int i=0;
					
				query[0]=getchar();				
				while( (query[i]=getchar()) !='\n' )
					i++;
				query[i+1]='\0';
					
				printf("-%d-",strlen(query));
				printf("\nQ: %s\n",query);
				 
				
				make_query(&msg, query);
					//printf("\nl:%d\n",strlen(query));
				http_send_rqst(boot_peer[0].peersock, &msg, &rspn);							 
			
				 //printf("E %s\n",rspn.entity);
				
				/* struct file_list *hit;
				 hit=(struct file_list*)malloc(sizeof(rspn.entity));
				 hit=(struct file_list*)&rspn.entity;
				 
					while(hit->next!=NULL){

							printf("file %s\n",hit->file->name);
						//	printf("bp\n");
							hit=hit->next;
							}*/
				 
				// printf("E%s\n",rspn.entity);
				plist(rspn.entity);
							
				// http_send_rqst(boot_peer[1].peersock, &msg, &rspn);

				 
				
			//	printf("E %s\n",msg.entity);
				// free(msg.entity);
				 //printf("Ee %s\n",msg.entity);


			break;			
			}
			
			case 4:{
				//
				
			break;	
			}
			
			case 5:{//update filelist
				while(shared_files->next!=NULL){
					while(shared_files->token_file_name->next!=NULL){
						free(shared_files->token_file_name->tok);
						shared_files->token_file_name=shared_files->token_file_name->next;
					}
					free(shared_files->token_file_name);
					free(shared_files->file->name);			
					free(shared_files->file);	
					shared_files=shared_files->next;
				}
			
				free(shared_files);
				shared_files= create_list(conf->sharefolder);
				
			break;				
			}
			
			case 6:{
				int k=0;
				for(;k<numconn;k++){
					close(boot_peer[k].peersock);}
				exit(1);
			break;
			
			}
			
		}
	
	}
}

void make_query(struct http_rqst_msg *msg, char *q){
	
	/*if(msg->entity!=NULL)
		free(msg->entity);*/
	strcpy(msg->request.method,http_rqst_method_string[HTTP_STR_RQST_METH_THEP2P]);
	msg->entity=(char*)malloc(sizeof(q));
	
	sprintf(msg->ent_header.cont_len,"%d",strlen(q));
	strcpy(msg->entity,q);
	strcpy(msg->request.uri,http_rqst_thep2p_string[HTTP_STR_RQST_THEP2P_QUERY]);
	
/*
	strcpy(msg->request.method,http_rqst_method_string[HTTP_STR_RQST_METH_THEP2P]);
	//msg->request.method_code;
	strcpy(msg->request.uri,http_rqst_thep2p_string[HTTP_STR_RQST_THEP2P_CONNECT]);
	*/
	
	
};

void plist(char *ent ){
	
	char *set=";,";
	char *token;
	
	token = strtok(ent,set);//printf("e:%s\n",token);
	//token = strtok(NULL,set);
	while(strcmp(token,"#")!=0){
		
		printf("\nN: %-45s ",token);
		token=strtok(NULL,set);
		printf("\tT: %-6s ",token);
		token=strtok(NULL,set);
		printf("\tS: %s ",token);
		//token_field=strtok(NULL,setf);
		//printf("t: %s\n",token_field);
		token = strtok(NULL,set);
	}

	
}